<?php namespace Common\Model;
use Hdphp\Model\Model;
//商品操作模型
class Goods extends  Model{
	//指定模型操作的表（固定的鞋法）
	protected $table="goods";
	//自动验证（固定写法）需要通过create方法才触发
	protected $validate=array(
			array('gname','required','商品名称不能为空',3,3)
	);
	//自动完成需要通过create方法才能触发
	protected $auto=array(
			array('time','time','function',3,1),
			array('user_uid','uid','method',3,2),
			array('pic','picAdd','method',3,3),
			array('type_tid','tid','method',3,3)
	);
	public function tid(){
		if(Q('post.class_cid')){
			$cid = Q('post.class_cid');
			$cate = new \Common\Model\Cate;
			return $cate->where("cid={$cid}")->pluck("type_tid");
		}
	}
	/**
	 * 自定义图片自动上传
	 */
	public function picAdd(){
		if(Q('post.pic')){
			$pic = Q('post.pic',array());
			$pic = implode(',', $pic);
			return $pic;
		}
		return '';
	}
	public function uid(){
		return $_SESSION['uid'];
	}
	//删除商品
	public function del(){
		
	    $this->where("gid={$gid}")-delete();
	}
	//添加
	public function addData(){
		
		//验证失败 返回假
		if(!$this->create()){
			return false;
		}
		
		//添加商品表
		$gid = $this->add();
		//add()方法可以返回主键id
		//获得get传递过来的gid
		//添加商品详细表
		$goodDataModel = new \Common\Model\GoodsData;
		//验证

		$goodData = array(
			"big" => implode(',', Q("post.big",array())),
			"intro" => Q("post.intro",''),
			"service" => Q("post.service",''),
			"goods_gid" => $gid
		);

		$goodDataModel->add($goodData);

		
		$goodsAttrModel = new \Common\Model\GoodsAttr;
		$data = $_POST;

		
		foreach($data['attr'] as $k=>$v){
			$arr = array(
				'gtvalue'=>$v,
				'added'=>0,
				'typeAttribute_aid'=>$k,
				'goods_gid'=>$gid
			);
			$goodsAttrModel->add($arr);
		}
		
		foreach($data['spec'] as $k=>$v){
			foreach($v['tavalue'] as $kk=>$vv){
				$arr = array(
					'gtvalue'=>$vv,
					'added'=>$v['added'][$kk],
					'typeAttribute_aid'=>$k,
					'goods_gid'=>$gid
				);
				$goodsAttrModel->add($arr);
			}
		}	
		return true;
		
	}
	//编辑商品
//	public function editData(){
//		//验证
//		if(!$this->create()) return false;
//		$goodDataModel = new \Admin\Model\GoodData;
//		if(!$goodDataModel->create()){
//			$this->error = $goodDataModel->getError();
//			return false;
//		}
//		//修改商品表
//		$this->save();
//		//取得隐藏域提交过来的gid
//		$gid = Q("post.gid","intval");
//		//修改商品属性表
//		$tid=Q("post.tid",0,"intval");
//		$joinAttrData=Db::table("goods_property")->join(
//		"type_property","type_property_tid","=","tpid")->where("good_gid={$gid}")->get;
//		$goodDataModel->where("goods_gid={$gid}")->save();
//		return true;
//	}
	public function editData(){
		if(!$this->create()) return false;
		$this->save();
		// 一,获得旧数据
		$gid = Q('get.gid',0,'intval');
		$goodsAttrModel = new \Common\Model\GoodsAttr;
		$goodsAttrModel->where("goods_gid={$gid}")->delete();
		$data = $_POST;
		foreach($data['attr'] as $k=>$v){
			$arr = array(
				'gtvalue'=>$v,
				'added'=>0,
				'typeAttribute_aid'=>$k,
				'goods_gid'=>$gid
			);
			$goodsAttrModel->add($arr);
		}
		foreach($data['spec'] as $k=>$v){
			foreach($v['tavalue'] as $kk=>$vv){
				$arr = array(
					'gtvalue'=>$vv,
					'added'=>$v['added'][$kk],
					'typeAttribute_aid'=>$k,
					'goods_gid'=>$gid
				);
				$goodsAttrModel->add($arr);
			}
		}
		$DetailModel = new \Common\Model\GoodsData;
		$did = $DetailModel->where("goods_gid={$gid}")->pluck("did");
		$arr = array(
			"big" => implode(',', Q("post.big",array())),
			"intro" => Q("post.intro",''),
			"service" => Q("post.service",''),
			"goods_gid" => $gid
		);
		$DetailModel->where("did={$did}")->update($arr);
		
		return true;
	}
	
}


 ?>